#include "common.h"

#include "FileReader.h"
#include "Utils.h"

void printHelp(){
	cout<<"..."<<endl; //+++
}
int main(int argc,char *argv[])
{
	unsigned int i,j;
	bool DirMode;
	string pathDataset;
	string Text;
	string inPath;
	string outPath;
	string textPath;
	ofstream outFile;
	ifstream textFile;
	FileReader* fr;
	uint8_t* BinText;
	char tempc;
	uint32_t temp32;
	string tempString;
	uint8_t headerSpace[10];
	unsigned int BinTextLen;
	vector<string> Files;
	uint32_t writtenTracks;
	bool firstData;
	unsigned int NSample;
	char Format;
	unsigned int FormatSize;
	
	
	if(argc==2 && (string(argv[1]).compare("-h") || 
					string(argv[2]).compare("--help"))){
		printHelp();
		exit(0);
	}
	if (argc < 4){
		cout << "error in command line" << endl;
		printHelp();
		exit(2);
	}
	if(string(argv[1]).compare("-d")==0){
		DirMode=1;
		if (argc != 5){
			cout << "error in command line" << endl;
			printHelp();
			exit(2);
		}
		inPath = string(argv[2]);
		textPath = string(argv[3]);
		outPath=argv[4];
	}else{
		DirMode=0;
		if (argc != 4){
			cout << "error in command line" << endl;
			printHelp();
			exit(2);
		}
		inPath = string(argv[1]);
		Text = string(argv[2]);
		outPath=argv[3];
	}
	if(!DirMode){
		try{//+++ only 1 track (first) from only 1 file
			fr = new FileReader(inPath);
		}catch(const char* e){
			cout<<"*** error reading file"<<inPath<<": ";
			cout<<e<<endl;
			exit(1);
		}
		if(!Utils::isHexText(Text)){
			cout<<"*** "<<Text<<" is not a valid hex string"<<endl;
			exit(2);
		}
		BinTextLen=Text.size()/2;
		BinText = new uint8_t[BinTextLen];
		Utils::HexToBin(Text,BinText);
		outFile.open(outPath.c_str());
		if(!outFile.is_open()){
			cout<<"*** "<<outPath<<": unable to open file"<<endl;
			exit(1);
		}
		temp32=1;
		outFile.write((const char*)&temp32,4);
		temp32=fr->getNSample();
		outFile.write((const char*)&(temp32),sizeof(uint32_t));
		tempc=fr->getFormatType();
		outFile.write((const char*)&tempc,1);
		outFile.write((const char*)&(BinTextLen),sizeof(uint8_t));
		outFile.write((const char*)fr->getTracks().at(0),
					fr->getFormatSize()*fr->getNSample());
		outFile.write((const char*)BinText,BinTextLen);
		outFile.close();
        delete BinText;
        delete fr;
    }else{
		try{
			Files=Utils::getDirFiles(inPath);
		}catch(const char* e){
			cout<<"*** error opening "<<inPath<<": "<<e<<endl;
			exit(1);
		}
		outFile.open(outPath.c_str());
		if(!outFile.is_open()){
			cout<<"*** "<<outPath<<": unable to open file"<<endl;
			exit(1);
		}
		textFile.open(textPath.c_str());
		if(!textFile.is_open()){
			cout<<"*** "<<textPath<<": unable to open file"<<endl;
			exit(1);
		}
		outFile.write((const char*)headerSpace,10);
		for(i=0,writtenTracks=0,firstData=1;i<Files.size();i++){
			try{//+++ only 1 track (first) from only 1 file
				if(*(inPath.c_str()+inPath.size()-1)=='/'){
					tempString=inPath+Files[i];
				}else{
					tempString=inPath+"/"+Files[i];
				}
				cout<<"reading file: "<<tempString<<" ..."<<endl;
				fr = new FileReader(tempString);
			}catch(const char* e){
				cout<<"*** error reading file "<<tempString<<": ";
				cout<<e<<endl;
				continue;
			}
			for(j=0;j<fr->getTracks().size();j++){//+++ now always 1
				getline(textFile,Text);
				Text=Utils::CleanString(Text);
				//cout<<Text<<endl;
				if(!Utils::isHexText(Text)){
					cout<<"*** line "<<writtenTracks+1<<" of "<<
								textPath<<" is not an hex string"<<endl;
					exit(1);
				}//+++ not enough ***
				if(writtenTracks==0){
					NSample=fr->getNSample();
					Format=fr->getFormatType();
					FormatSize=fr->getFormatSize();
					BinTextLen=Text.size()/2;
					BinTextLen=Text.size()/2;
				}
				BinText = new uint8_t[BinTextLen];
				Utils::HexToBin(Text,BinText);
				outFile.write((const char*)fr->getTracks().at(j),
							NSample*FormatSize);
				outFile.write((const char*)BinText,BinTextLen);
				writtenTracks++;
				delete BinText;
				delete fr;
			}
		}
		outFile.seekp (0);
		temp32=writtenTracks;
		outFile.write((const char*)&temp32,4);
		temp32=NSample;
		outFile.write((const char*)&(temp32),sizeof(uint32_t));
		tempc=Format;
		outFile.write((const char*)&tempc,1);
		outFile.write((const char*)&(BinTextLen),sizeof(uint8_t));
		outFile.close();
		//cout<<"."<<writtenTracks<<"."<<NSample<<"."<<Format<<"."<<BinTextLen<<endl;
	}
}






